package lvm

import (
	"context"
	"fmt"
)

// LVCreate 创建逻辑卷
func LVCreate(ctx context.Context, vgName, lvName string, sizeM int64) (lv *LogicalVolume, err error) {
	vg := getVG(ctx, vgName)
	if vg == nil {
		err = ErrNotFound
		return
	}
	if vg.FreeSize < (sizeM * 1000 * 1000) {
		err = ErrNoEnoughSpace
		return
	}

	if getLV(ctx, vgName, lvName) != nil {
		err = ErrExists
		return
	}

	_, err = execCommand(ctx, "lvcreate", "--size", fmt.Sprintf("%dm", sizeM), "-y", "-n", lvName, vgName)
	if err != nil {
		return
	}
	lv = getLV(ctx, vgName, lvName)
	if lv == nil {
		err = ErrCommand
	}
	return
}
